{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys,os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#下面生成顺序的trainval.txt和labels文件\n",
    "# 先设置train占数据集的百分比，余下部分为val\n",
    "maindir='image\\';\n",
    "wf = fopen('trainval.txt','w');\n",
    "lbf=fopen('labels.txt','w');\n",
    "train_percent=0.9;%val_percent=1-train_percent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path1 = r\"C:/Users/l/Desktop/CSDN培训资料/第9轴_卷积神经网络分类任务和检测任务/W9_object_detection/\"  \n",
    "data_path = os.path.join(path1,'object_detection/dataset_tools/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 26)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<tokenize>\"\u001b[1;36m, line \u001b[1;32m26\u001b[0m\n\u001b[1;33m    end\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "clc;\n",
    "clear;\n",
    "%%下面生成顺序的trainval.txt和labels文件\n",
    "%先设置train占数据集的百分比，余下部分为val\n",
    "maindir='image\\';\n",
    "wf = fopen('trainval.txt','w');\n",
    "lbf=fopen('labels.txt','w');\n",
    "train_percent=0.9;%val_percent=1-train_percent\n",
    "\n",
    "subdir = dir(maindir);\n",
    "ii=-1;\n",
    "numoffile=0;\n",
    "for i = 1:length(subdir)%第一层目录\n",
    "  if ~strcmp(subdir(i).name ,'.') && ~strcmp(subdir(i).name,'..')\n",
    "     ii=ii+1;\n",
    "     label = subdir(i).name;\n",
    "     fprintf(lbf,'%s\\n',label);\n",
    "     label=strcat(label,'/');\n",
    "     subsubdir = dir(strcat(maindir,label));\n",
    "     for j=1:length(subsubdir)\n",
    "         if ~strcmp(subsubdir(j).name ,'.') && ~strcmp(subsubdir(j).name,'..')\n",
    "           fprintf(wf,'%s%s%s %d\\n','image/',label,subsubdir(j).name,ii);\n",
    "           numoffile=numoffile+1;\n",
    "           fprintf('处理标签为%d的第%d张图片\\n',ii,j-2);\n",
    "         end\n",
    "    end\n",
    "   \n",
    "  end\n",
    "end\n",
    "fclose(wf);\n",
    "fclose(lbf);\n",
    "\n",
    "%%\n",
    "%下面将trainval的顺序打乱\n",
    "file=cell(1,numoffile);\n",
    "fin=fopen('trainval.txt','r');\n",
    "i=1;\n",
    "while ~feof(fin)\n",
    "    tline=fgetl(fin);\n",
    "    file{i}=tline;\n",
    "    i=i+1;\n",
    "end\n",
    "fclose(fin);\n",
    "\n",
    "fprintf('\\ntrainval.txt共%d行，开始打乱顺序....\\n',numoffile);\n",
    "pause(1);\n",
    "rep=randperm(numoffile);\n",
    "fout=fopen('trainval.txt','w');\n",
    "for i=1:numoffile\n",
    "    fprintf(fout,'%s\\n',file{rep(i)});\n",
    "end\n",
    "fprintf('生成的trainval.txt已打乱顺序.\\n');\n",
    "fclose(fout);\n",
    "\n",
    "%%\n",
    "%下面根据打乱顺序的trainval.txt生成train.txt和val.txt\n",
    "fprintf('开始生成train.txt和val.txt...\\n');\n",
    "pause(1);\n",
    "train_file=fopen('train.txt','w');\n",
    "text_file=fopen('val.txt','w');\n",
    "trainvalfile=fopen('trainval.txt','r');\n",
    "\n",
    "num_train=sort(randperm(numoffile,floor(numoffile*train_percent)));\n",
    "num_test=setdiff(1:numoffile,num_train);\n",
    "i=1;\n",
    "while ~feof(trainvalfile)\n",
    "    tline=fgetl(trainvalfile);\n",
    "    if ismember(i,num_train)\n",
    "        fprintf(train_file,'%s\\n',tline);\n",
    "    else\n",
    "        fprintf(text_file,'%s\\n',tline);\n",
    "    end\n",
    "    i=i+1;\n",
    "end\n",
    "fclose(train_file);\n",
    "fclose(text_file);\n",
    "fclose(trainvalfile);\n",
    "fprintf('共有图片%d张!\\n',numoffile);\n",
    "fprintf('Done！\\n');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
